/*
 * MenuDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "MenuDelegate.h"

// MenuDelegate :: Protected Constructor. --------------------------------------

/*!
 * \brief Constructs new MenuDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the Menu java object
 */
MenuDelegate::MenuDelegate(JNIEnv *env, jobject obj)
  : ContainerDelegate(env, obj, new QMenu())
{
  qMenu()->setSeparatorsCollapsible(false);
}

// MenuDelegate :: Native Calls. -----------------------------------------------

/*!
 * \brief Sets menu text
 *
 * \param env the JNIEnv pointer
 * \param text the text
 */
void MenuDelegate::setText(JNIEnv *env, jstring text)
{
  Q_ASSERT(env);
  Q_ASSERT(text);
  SWING_REENTRANT(setText, SWING_ENV_ARG, JSTRING_ARG(text))
  qMenu()->setTitle(QStringAdapter(env, text));
}

/*!
 * \brief Inserts child.
 *
 * \param env the JNIEnv pointer
 * \param childDelegate the child delegate
 * \param index the menu item index
 */
void MenuDelegate::insertChild(JNIEnv *env, jlong childDelegate, jint index)
{
  Q_ASSERT(env);
  SWING_REENTRANT(insertChild, SWING_ENV_ARG, JLONG_ARG(childDelegate),
                  JINT_ARG(index))
  QAction *before = 0;
  if ((index >= 0) && (index < qMenu()->actions().size())) {
    before = qMenu()->actions().at(index);
  }
  ComponentDelegate *childComponentDelegate = COMPONENT_CAST(childDelegate);
  qMenu()->insertAction(before, childComponentDelegate->qAction());
  childComponentDelegate->setParentDelegate(this);
  childComponentDelegate->setParent(this);
}

/*!
 * \brief Removes child.
 *
 * \param env the JNIEnv pointer
 * \param childDelegate the child delegate
 */
void MenuDelegate::removeChild(JNIEnv *env, jlong childDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(removeChild, SWING_ENV_ARG, JLONG_ARG(childDelegate))
  ComponentDelegate *childComponentDelegate = COMPONENT_CAST(childDelegate);
  qMenu()->removeAction(childComponentDelegate->qAction());
  childComponentDelegate->setParentDelegate(0);
  childComponentDelegate->setParent(0);
}

/*!
 * \brief Displays menu at specified location.
 *
 * \param env the JNIEnv pointer
 * \param x the x coordinate of the location
 * \param y the y coordinate of the location
 */
void MenuDelegate::show(JNIEnv *env, jint x, jint y)
{
  Q_ASSERT(env);
  SWING_REENTRANT(show, SWING_ENV_ARG, JINT_ARG(x), JINT_ARG(y))
  qMenu()->popup(QPoint(x, y));
}

// MenuDelegate :: Static Native Calls. ----------------------------------------

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong MenuDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new MenuDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}

// MenuDelegate :: Overridden Member Function. ---------------------------------

/*!
 * \inheritDoc
 */
QAction *MenuDelegate::qAction()
{
  return qMenu()->menuAction();
}
